<?php
class App_User_Model_Role extends Main_Db_Table_Abstract
{
	/**
	 * @var string
	 */
	protected $_name = 'role';
	/**
	 * @var string
	 */
	protected $_primary = 'id';
	
	/**
	 * @var array
	 */
	protected $_fields = array(
		'id',
		'code'
	);
	
	/**
	 * @var App_User_Model_RoleI18n
	 */
	protected $_modelRoleI18n;	
	
	/**
	 * @return void
	 */
	public function init() 
	{
		$this->_modelRoleI18n = Main_Model::factory('User_Model_RoleI18n');
	}
	
	/**
	 * @param array $data
	 * @return bool
	 */
	public function insertItem($data) 
	{
		$dataOk = $this->beforeSave($data, array('code'), array('code'));
		
		$result = $this->insert($dataOk);
				
		$data['role_id'] = $this->lastInsertId();
		$res1 = $this->_modelRoleI18n->insertItem($data);
		
		return $result && $res1;
	}
	
	/**
	 * @param array $data
	 * @param int $id
	 * @return bool
	 */
	public function updateItem($data, $id)
	{
		$id = $this->escape($id);
		$dataOk = $this->beforeSave($data, array('code'), array('code'));
    	
		$res1 = $this->_modelRoleI18n->updateItem($data, $id);  	
		$res2 = $this->update($dataOk, "id={$id}");
    	return $res1 && $res2;
	}
	
	/**
	 * @param int $id
	 * @param int $langId
	 * @return array
	 */
	public function getItemById($id, $langId) 
	{
		$id = $this->escape($id);
		$langId = $this->escape($langId);
		
		$sql = "
			SELECT 
				r.id
				, r.code
				, r_i18n.name
			FROM
				role r
			INNER JOIN
				role_i18n r_i18n
				ON
					(r.id=r_i18n.role_id AND r_i18n.lang_id={$langId})
			WHERE
				r.id={$id}		
		";
				
		$list = $this->getAdapter()->fetchRow($sql);

		return $list;
	}
	
	/**
	 * @param int $langId
	 * @return array
	 */
	public function getAll($langId) 
	{
		$sql = "
			SELECT 
				r.id
				, r.code
				, r_i18n.name
			FROM
				role r
			INNER JOIN
				role_i18n r_i18n
				ON
					(r.id=r_i18n.role_id AND r_i18n.lang_id={$langId})
		";
		$list = $this->getAdapter()->fetchAll($sql);
		
		return $list;
	}
	
	
	/**
	 * @param int $langId
	 * @return array
	 */
	public function getAllIdAndName($langId) 
	{
		/*$langId = $this->escape($langId);
		$sql = "
			SELECT 
				r.id
				, r.code
				, r_i18n.name
			FROM
				role r
			INNER JOIN
				role_i18n r_i18n
				ON
					(r.id=r_i18n.role_id AND r_i18n.lang_id={$langId})
		";
		$list = $this->getAdapter()->fetchAll($sql);*/
		$list = $this->getAll($langId);
		
		$res = Main_Function::expandList($list, 'id', 'name');

		return $res;
	}
	
	/**
	 * @param string $code
	 * @param int $langId
	 * @return array
	 */
	public function getItemByCode($code, $langId) 
	{
		$code = $this->escape($code, true);
		$langId = $this->escape($langId);
		
		$sql = "
			SELECT 
				r.id
				, r.code
				, r_i18n.name
			FROM
				role r
			INNER JOIN
				role_i18n r_i18n
				ON
					(r.id=r_i18n.role_id AND r_i18n.lang_id={$langId})
			WHERE
				r.code={$code}		
		";
				
		$list = $this->getAdapter()->fetchRow($sql);

		return $list;
	}
		
	/**
	 * @param int $id
	 * @return bool
	 */
	public function deleteItem($id) 
	{
		$id = $this->escape($id);
		if ($this->_modelRoleI18n->deleteItemByRoleId($id)) {
			return $this->delete("id={$id}");
		} else {
			return false;
		}	
	}
	
} 
